package com.dces.jx.billtest.api.zhj;
import com.dces.jx.billtest.common.service.ExcelService;
import com.dces.jx.billtest.dto.zhj.*;
import com.dces.jx.billtest.userAdd.model.DcBillListZhj;
import com.dces.jx.billtest.userAdd.service.DcBillListZhjService;
import com.dces.jx.billtest.zhj.mapper.DcBillHeadZhjDtoMapper;
import com.dces.jx.billtest.zhj.model.DcBillHeadZhj;
import com.dces.jx.billtest.zhj.model.StatusInfo;
import com.dces.jx.billtest.zhj.model.Task;
import com.dces.jx.billtest.zhj.service.DcBillHeadZhjService;
import com.xdo.pcode.service.PCodeHolder;
import com.xdo.domain.PageParam;
import com.xdo.domain.ResultObject;
import com.xdo.common.token.UserInfoToken;
import com.xdo.springboot.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * generated by Generate dc
 * 
 *
 * @author xdoV2.0
 * @date: 2021-9-3
 */
@RestController
@RequestMapping("v1/dcBillHeadZhj")
@Api(tags = "接口")
public class DcBillHeadZhjController extends BaseController {
    @Resource
    private DcBillHeadZhjService dcBillHeadZhjService;
    @Resource
    private DcBillListZhjService dcBillListZhjService;
    @Resource
    private DcBillHeadZhjDtoMapper dcBillHeadZhjDtoMapper;
    @Resource
    private PCodeHolder pCodeHolder;
    @Resource
    private ExcelService excelService;
    /**
     * @param dcBillHeadZhjParam
     * @param pageParam
     * @param userInfo
     * @return
     */
    @ApiOperation("分页查询接口")
    @PostMapping("list")
    public ResultObject<List<DcBillHeadZhjDto>> getListPaged(@RequestBody DcBillHeadZhjParam dcBillHeadZhjParam, PageParam pageParam, UserInfoToken userInfo) {
        ResultObject<List<DcBillHeadZhjDto>> paged = dcBillHeadZhjService.getListPaged(dcBillHeadZhjParam, pageParam);
        return paged;
    }
    /**
     * @param dcBillHeadZhjParam
     * @param userInfo
     * @return
     */
    @ApiOperation("新增接口")
    @PostMapping()
    public ResultObject<DcBillHeadZhjDto> insert(@Valid @RequestBody DcBillHeadZhjParam dcBillHeadZhjParam, UserInfoToken userInfo) {
        /**
         * 后台字段处理
         */
        DcBillHeadZhj dcBillHeadZhj = dcBillHeadZhjService.getDcBillHeadZhj(dcBillHeadZhjParam);

        ResultObject<DcBillHeadZhjDto> resultObject = ResultObject.createInstance(true, ResultObject.INSERT_SUCCESS);
        int insert = dcBillHeadZhjService.insert(dcBillHeadZhj);
        if(insert==1){
            resultObject.setData(dcBillHeadZhjDtoMapper.toDto(dcBillHeadZhj));
        }else{
            resultObject.setSuccess(false);
            resultObject.setMessage(ResultObject.INSERT_FAIL);
        }

        return resultObject;
    }
    /**
     * @param sid
     * @param dcBillHeadZhjParam
     * @param userInfo
     * @return
     */
    @ApiOperation("修改接口")
    @PutMapping("{sid}")
    public ResultObject<DcBillHeadZhjDto> update(@PathVariable String sid, @Valid @RequestBody DcBillHeadZhjParam dcBillHeadZhjParam, UserInfoToken userInfo) {
        dcBillHeadZhjParam.setSid(sid);
        DcBillHeadZhjDto dcBillHeadZhjDto = dcBillHeadZhjService.update(dcBillHeadZhjParam, userInfo);
        ResultObject resultObject = ResultObject.createInstance(true, ResultObject.UPDATE_SUCCESS);
        if (dcBillHeadZhjDto != null) {
            resultObject.setData(dcBillHeadZhjDto);
        } else {
            resultObject.setSuccess(false);
            resultObject.setMessage(ResultObject.UPDATE_FAIL);
        }
        return resultObject;
    }
    /**
     * @param sids
     * @return
     */
    @ApiOperation("删除接口")
    @DeleteMapping("{sids}")
    public ResultObject delete(@PathVariable List<String> sids, UserInfoToken userInfo) {
        ResultObject resultObject = ResultObject.createInstance(true, ResultObject.DELETE_SUCCESS);
        // 检查是否存在表体数据
		dcBillHeadZhjService.delete(sids, userInfo);
        return resultObject;
    }
    /**
     * @param exportParam
     * @param userInfo
     * @return
     * @throws Exception
     */
    @ApiOperation("Excel数据导出接口")
    @PostMapping(value = "/export")
    public ResponseEntity export(@Valid @RequestBody DcBillHeadZhjExportParam exportParam, UserInfoToken userInfo) throws Exception{
        List<DcBillHeadZhjDto> dcBillHeadZhjDtos = dcBillHeadZhjService.selectAll(exportParam.getExportColumns(), userInfo);
        convertForPrint(dcBillHeadZhjDtos);
        return excelService.getExcelHeaders(URLEncoder.encode(exportParam.getName(), "utf-8"), exportParam.getHeader(), dcBillHeadZhjDtos, userInfo);
    }
    /**
     * 导出，pCode转换中文名称
     * @param list
     * @return
     */
    public void convertForPrint(List<DcBillHeadZhjDto> list) {
        for(DcBillHeadZhjDto item : list) {
        }
    }
    @ApiOperation("状态清单查询")
    @GetMapping("/queryListStatus/{status}")
    public ResultObject queryListStatus(@PathVariable("status") String status){
        List<StatusInfo> list = dcBillHeadZhjService.queryListStatus(status);
        return ResultObject.createInstance(true,"查询数据成功",list);
    }

    @ApiOperation("状态清单查询")
    @GetMapping("/queryEntryStatus/{status}")
    public ResultObject queryEntryStatus(@PathVariable("status") String status){
        List<StatusInfo> list = dcBillHeadZhjService.queryEntryStatus(status);
        return ResultObject.createInstance(true,"查询数据成功",list);
    }
    @ApiOperation("下拉框状态查询")
    @GetMapping("/selectStatus/{status}")
    public ResultObject querySelectStatus(@PathVariable("status") String status){
        List<StatusInfo> list = dcBillHeadZhjService.querySelectStatus(status);
        return ResultObject.createInstance(true,"查询数据成功",list);
    }

    @Transactional
    @ApiOperation(("删改恢复"))
    @PostMapping("/recovery")
    public ResultObject recovery(@Valid @RequestBody DcBillHeadZhjParam dcBillHeadZhjParam) throws ParseException {

        return dcBillHeadZhjService.recovery(dcBillHeadZhjParam);
    }
    @ApiOperation(("根据headId获取表体有多少条数据"))
    @GetMapping("/ListNoByHeadId")
    public ResultObject listNoByHeadId(@RequestParam String headId){
        return dcBillHeadZhjService.getListNoByHeadId(headId);
    }

    @Transactional
    @ApiOperation(("保存前的校验"))
    @PostMapping("/beforeHandleSave")
    public ResultObject beforeHandleSave(@RequestBody BeforeSaveParam beforeSaveParam){
        return dcBillHeadZhjService.beforeHandleSave(beforeSaveParam);
    }
    @Transactional
    @ApiOperation(("保存表头表体数据"))
    @PostMapping("/BackupHeadAndList")
    public ResultObject backupHeadAndList(@Valid @RequestBody DcBillHeadZhjParam dcBillHeadZhjParam) throws ParseException {
        return dcBillHeadZhjService.backupHeadAndList(dcBillHeadZhjParam);
    }


}
